package com.ydche.client;

import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.thrift.protocol.TProtocol;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class KernelClient extends RpcClient {

    @Value("${thrift.kernel.host}")
    private String host;
    @Value("${thrift.kernel.port}")
    private int port;
    @Value("${thrift.kernel.timeout}")
    private int timeout;

    private GenericObjectPool<TProtocol> objectPool;
    @Override
    public void propertiesSet() {
        if (this.host == null){
            defaultProperties();
        }
        super.setIp(this.host);
        super.setPort(this.port);
        super.setTimeout(this.timeout);
     }

    private void defaultProperties(){
        this.host = "127.0.0.1";
        this.port = 3007;
        this.timeout = 5000;
    }

    @Override
    public TProtocol getConnection() {
        try {
            return objectPool.borrowObject();
        } catch (Exception e) {
            throw new RuntimeException("getConnection异常", e);
        }
    }

    @Override
    public void returnConnection(TProtocol tProtocol) {
        objectPool.returnObject(tProtocol);
    }

    public KernelClient(){
        this.propertiesSet();
        objectPool = new GenericObjectPool<TProtocol>(this);
//        IKernelRpcService.Iface kernelClient = new IKernelRpcService.Client(this.getConnection());
//        try {
//            RiskResp riskResp = kernelClient.GetRiskList();
//            for (RiskInfo risk :riskResp.riskList){
//                System.out.println( risk.riskName);
//            }
//        } catch (TException e) {
//            e.printStackTrace();
//        }
    }
}
